/**
 * Copyright (C), 2015-2021, XXX有限公司
 * FileName: UpVideoActivity
 * Author: Gxjkz
 * Date: 2021/7/30 10:21
 * Description: 上传视频
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
package com.niuxiaofa.newlaw.Activity;

import android.content.Intent;
import android.os.Build;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;
import com.niuxiaofa.newlaw.Base.BaseActivity;
import com.niuxiaofa.newlaw.Bean.BaseBean;
import com.niuxiaofa.newlaw.Network.PublicPresenter;
import com.niuxiaofa.newlaw.R;
import com.niuxiaofa.newlaw.Tools.GetLog;
import com.niuxiaofa.newlaw.Tools.LogUtil;
import com.niuxiaofa.newlaw.videoupload.TXUGCPublish;
import com.niuxiaofa.newlaw.videoupload.TXUGCPublishTypeDef;

import java.util.List;

import static com.luck.picture.lib.config.PictureConfig.CHOOSE_REQUEST;

/**
 * @ClassName: UpVideoActivity
 * @Description: 上传视频
 * @Author: Gxjkz
 * @Date: 2021/7/30 10:21
 */
public class UpVideoActivity extends BaseActivity {

    TXUGCPublish mVideoPublish;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_upvideo;
    }

    String key = "";

    @Override
    protected void initData() {

        PublicPresenter.getUploadSignature(new PublicPresenter.interface_getUploadSignature() {
            @Override
            public void getResponse(BaseBean u) {

                key = u.getData().toString();

            }
        });


        mVideoPublish = new TXUGCPublish(this.getApplicationContext(), "independence_android");


        mVideoPublish.setListener(new TXUGCPublishTypeDef.ITXVideoPublishListener() {
            @Override
            public void onPublishProgress(long uploadBytes, long totalBytes) {
                //   mProgress.setProgress((int) (100*uploadBytes/totalBytes));

                GetLog.getLog("totalBytes" + totalBytes + "");
                GetLog.getLog("上传进度" + (int) (100 * uploadBytes / totalBytes));


            }

            @Override
            public void onPublishComplete(TXUGCPublishTypeDef.TXPublishResult result) {
                //  mResultMsg.setText();

                GetLog.getLog(result.retCode + " Msg:" + (result.retCode == 0 ? result.videoURL : result.descMsg));

            }
        });


//        PictureSelector.create(UIUtils.getActivity())
//                .openGallery(PictureMimeType.ofVideo())
//                .maxSelectNum(1)
//                .theme(R.style.picture_my_style)
//                .loadImageEngine(GlideEnCHOOSE_REQUESTgine.createGlideEngine()) // 请参考Demo GlideEngine.java
//                .forResult();


    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == -1) {
            switch (requestCode) {

                case CHOOSE_REQUEST:
                    // 图片选择结果回调
                    List<LocalMedia> selectListPic = PictureSelector.obtainMultipleResult(data);
                    for (LocalMedia media : selectListPic) {
                        LogUtil.e("获取图片路径成功:" + media.getPath());

                        if (Build.VERSION.SDK_INT >= 29) {
                            media.setPath(media.getRealPath());
                        }


                        TXUGCPublishTypeDef.TXPublishParam param = new TXUGCPublishTypeDef.TXPublishParam();
                        param.signature = key;
                        param.videoPath = media.getPath();
                        int publishCode = mVideoPublish.publishVideo(param);

                        GetLog.getLog("publishCode" + publishCode + "");
                    }
                    break;

            }
        }
    }
}